﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;

namespace ModernShopping.AppCode
{
    public static class DbStore
    {
        public static void InitGridView(GridView gr, Guid UserID)
        {
            ModernShoppingDataContext db = new ModernShoppingDataContext();
            var results = db.Stores.Where(c => (c.CompanyCode.Equals(UserID) && c.isDeleted == false));
            gr.DataSource = results;
            gr.DataBind();
        }

        public static void InitGridView(GridView gr, int ProductCode)
        {
            ModernShoppingDataContext db = new ModernShoppingDataContext();
            var results = db.ProductStores.Where(c => (c.ProductCode.Equals(ProductCode)) && (c.Store.isDeleted == false));
            gr.DataSource = results.Select(c => c.Store);
            gr.DataBind();
        }

        public static void InitDropdownList(DropDownList cbo, Guid UserID)
        {
            ModernShoppingDataContext db = new ModernShoppingDataContext();
            var results = db.Stores.Where(c => (c.CompanyCode.Equals(UserID) && c.isDeleted == false));
            cbo.DataSource = results;
            cbo.DataTextField = "Name";
            cbo.DataValueField = "Code";
            cbo.DataBind();
        }

        public static void RefreshDropdownList(DropDownList cbo, Guid UserID, List<Store> listStores)
        {
            ModernShoppingDataContext db = new ModernShoppingDataContext();
            var results = db.Stores.Where(c => c.CompanyCode.Equals(UserID) && c.isDeleted == false);
            foreach (Store st in listStores)
            {
                results = results.Where(c => !c.Code.Equals(st.Code));
            }
            cbo.DataSource = results;
            cbo.DataTextField = "Name";
            cbo.DataValueField = "Code";
            cbo.DataBind();
        }
        public static void InitDropdownList(DropDownList cbo, Guid UserID, int ProductCode)
        {
            ModernShoppingDataContext db = new ModernShoppingDataContext();
            var listStoreOfProduct = db.ProductStores.Where(c => c.ProductCode.Equals(ProductCode));
            var listStore = db.Stores.Where(c => c.CompanyCode.Equals(UserID));
            foreach (ProductStore ps in listStoreOfProduct)
            {
                listStore = listStore.Where(c => !c.Code.Equals(ps.CodeStore) && c.isDeleted == false);
            }
            cbo.DataSource = listStore;
            cbo.DataTextField = "Name";
            cbo.DataValueField = "Code";
            cbo.DataBind();
        }

        public static Store GetByID(int ID)
        {
            ModernShoppingDataContext db = new ModernShoppingDataContext();
            Store st = db.Stores.Where(c => c.Code.Equals(ID) && c.isDeleted == false).FirstOrDefault();
            return st;
        }

        public static int getNumberOfStores(Guid UserID)
        {
            ModernShoppingDataContext db = new ModernShoppingDataContext();
            return db.Stores.Count(c => c.CompanyCode.Equals(UserID) && c.isDeleted == false);
        }

        public static bool AddNewStore(Store store, Guid CompanyCode)
        {
            try
            {
                ModernShoppingDataContext db = new ModernShoppingDataContext();
                store.CompanyCode = CompanyCode;
                store.isDeleted = false;
                db.Stores.InsertOnSubmit(store);
                db.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static List<Store> GetList(Guid CompanyCode)
        {
            try
            {
                ModernShoppingDataContext db = new ModernShoppingDataContext();
                return db.Stores.Where(c => c.CompanyCode.Equals(CompanyCode) && c.isDeleted == false).ToList<Store>();
            }
            catch
            {
                return null;
            }
        }

        public static List<Store> GetList(int productCode)
        {
            try
            {
                ModernShoppingDataContext db = new ModernShoppingDataContext();
                return db.ProductStores
                            .Where(c => c.ProductCode.Equals(productCode))
                            .Where(c => c.Store.isDeleted.Equals(false))
                            .Select(p => p.Store).ToList<Store>();
            }
            catch
            {
                return null;
            }
        }

        public static bool HandleMarker(List<Marker> listMarker, Guid CompanyCode)
        {
            try
            {
                ModernShoppingDataContext db = new ModernShoppingDataContext();
                foreach (Marker mk in listMarker)
                {
                    int Command = mk.Command;
                    Store store = mk.ToStore();
                    if (Command == Marker.INSERT)
                    {
                        store.CompanyCode = CompanyCode;
                        db.Stores.InsertOnSubmit(store);
                    }
                    else if (Command == Marker.DELETE)
                    {
                        var tmp = db.Stores.Where(c => c.Code.Equals(store.Code)).FirstOrDefault();
                        tmp.isDeleted = true;
                    }
                    else if (Command == Marker.UPDATE)
                    {
                        var tmp = db.Stores.Where(c => c.Code.Equals(store.Code)).FirstOrDefault();
                        tmp.Name = store.Name;
                        tmp.Address = store.Address;
                        tmp.Longitude = store.Longitude;
                        tmp.Latitude = store.Latitude;
                    }
                }
                db.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}